package com.mdd.admin.controller;

import com.mdd.admin.service.IRegionService;
import com.mdd.admin.validate.RegionCreateValidate;
import com.mdd.admin.validate.RegionUpdateValidate;
import com.mdd.admin.validate.RegionSearchValidate;
import com.mdd.common.aop.Log;
import com.mdd.common.aop.NotPower;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.IdValidate;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.system.vo.RegionDetailVo;
import com.mdd.system.vo.RegionListedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/region")
@Api(tags = "地区管理")
public class RegionController {

    @Resource
    IRegionService iRegionService;

    @GetMapping("/list")
    @ApiOperation(value="地区列表")
    public AjaxResult<PageResult<RegionListedVo>> list(@Validated PageValidate pageValidate,
                                                       @Validated RegionSearchValidate searchValidate) {
        PageResult<RegionListedVo> list = iRegionService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="地区详情")
    public AjaxResult<RegionDetailVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        RegionDetailVo detail = iRegionService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "地区新增")
    @PostMapping("/add")
    @ApiOperation(value="地区新增")
    public AjaxResult<Object> add(@Validated @RequestBody RegionCreateValidate createValidate) {
        iRegionService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "地区编辑")
    @PostMapping("/edit")
    @ApiOperation(value="地区编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody RegionUpdateValidate updateValidate) {
        iRegionService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "地区删除")
    @PostMapping("/del")
    @ApiOperation(value="地区删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iRegionService.del(idValidate.getId());
        return AjaxResult.success();
    }


    @NotPower
    @GetMapping("/listFirst")
    @ApiOperation(value="获取一级地区")
    public AjaxResult<List<RegionListedVo>> listFirst() {
        List<RegionListedVo> list = iRegionService.listFirst();
        return AjaxResult.success(list);
    }
    @NotPower
    @GetMapping("/listByParentId")
    @ApiOperation(value="获取下级地区")
    public AjaxResult<List<RegionListedVo>> listByParentId(@Validated @RequestParam("parentId") Long parentId) {
        List<RegionListedVo> list = iRegionService.listByParentId(parentId);
        return AjaxResult.success(list);
    }
}
